package com.nulldev.util.web.YouTubeExtractor.testing.tests;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.concurrency.threadIt.v4.emapi.ExecutorManager;
import com.nulldev.util.internal.backport.concurrency9.Lists;
import com.nulldev.util.internal.testing.Test;
import com.nulldev.util.web.YouTubeExtractor.YouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.YoutubeExtractionOptions;
import com.nulldev.util.web.YouTubeExtractor.extractors.IYouTubeExtractor;
import com.nulldev.util.web.YouTubeExtractor.items.YouTubeChapterEntry;
import com.nulldev.util.web.YouTubeExtractor.video.ExtractedVideo;

public class ExtractionTests {

	public static class ExtractorChapterTest extends Test<List<YouTubeChapterEntry>> {

		private final String VIDEO_ID;
		private final IYouTubeExtractor extractor;

		public ExtractorChapterTest(final String VIDEO_ID, final String extractorID) {
			this.VIDEO_ID = VIDEO_ID;
			this.extractor = YouTubeExtractor.get(extractorID);
		}

		@Override
		public void test() throws Throwable {
			final ExtractedVideo ev = ExecutorManager.get().queueTask(this.extractor.generateTask(this.VIDEO_ID, YoutubeExtractionOptions.NO_FALLBACK))
					.waitFor(30, TimeUnit.SECONDS);
			if (ev == null) {
				JVM.println("=> ExtractedVideo was null!");
				this.fail(Lists.of());
				return;
			} else if (!ev.getResult().successful()) {
				JVM.println("=> ExtractedVideo did not get extracted correctly!");
				this.fail(Lists.of());
			} else {
				if (!ev.getMetadata().hasChapters()) {
					JVM.println("=> ExtractedVideo did not get extract any chapters!");
					this.fail(Lists.of());
				}
				final List<YouTubeChapterEntry> chapters = ev.getMetadata().getChapters();
				if (chapters == null) {
					JVM.println("=> chapters was null!");
					this.fail(Lists.of());
				}
				if (chapters.isEmpty()) {
					JVM.println("=> chapters was empty!");
					this.fail(Lists.of());
				}
				this.finish(chapters);
			}
		}

	}
}
